package com.csu.pet.demo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csu.pet.demo.domain.Category;
import com.csu.pet.demo.domain.Item;
import com.csu.pet.demo.domain.Product;
import com.csu.pet.demo.domain.divide.Inventory;
import com.csu.pet.demo.domain.divide.ItemDetail;
import com.csu.pet.demo.persistence.CategoryMapper;
import com.csu.pet.demo.persistence.InventoryMapper;
import com.csu.pet.demo.persistence.ItemDetailMapper;
import com.csu.pet.demo.persistence.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.Provider;
import java.util.ArrayList;
import java.util.List;


@Service
public class CategoryService {
    //分别对应四张表的查询
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    InventoryMapper inventoryMapper;
    @Autowired
    ItemDetailMapper itemDetailMapper;
    @Autowired
    ProductMapper productMapper;

    //获得类的list
    public List<Category> getCategoryList() {
        List<Category> list = categoryMapper.selectList(null);
        System.out.println("查到的CategoryId有：");
        for (Category category:list) {
            System.out.println(category.getCategoryId());
        }
        return list;
    }

    //根据类的id获得类
    public Category getCategory(String categoryId) {
        Category category = categoryMapper.selectById(categoryId);
        System.out.println("查到的description：" + category.getDescription());
        return category;
    }

    //根据产品的id获得产品
    public Product getProduct(String productId) {
        Product product = productMapper.selectById(productId);
        System.out.println("查到的name是：" + product.getName());
        return product;
    }

    //根据类的id获得产品list
    public List<Product> getProductListByCategory(String categoryId) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        List<Product> list = productMapper.selectList(queryWrapper.eq("category",categoryId));
        System.out.println("查到的name有：");
        for (Product product:list) {
            System.out.println(product.getName());
        }
        return list;
    }

    //查找产品中有关键词keyword的产品list
    public List<Product> getProductList(String keyword) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        List<Product> list = productMapper.selectList(queryWrapper.like("name",keyword));
        System.out.println("查到的name有：");
        for (Product p:list) {
            System.out.println(p.getName());
        }
        return list;
    }

    //根据产品id获得商品list(Item和product两张表都要查，最后整合进item对象)
    public List<Item> getItemListByProduct(String productId) {
        //查item表
        QueryWrapper<ItemDetail> queryWrapper = new QueryWrapper<>();
        List<ItemDetail> itemDetails = itemDetailMapper.selectList(queryWrapper.eq("productid",productId));
        System.out.println("查到的ItemId有：");
        for (ItemDetail itemDetail:itemDetails) {
            System.out.println(itemDetail.getItemId());
        }
        //new一个itemlist
        List<Item> itemList = new ArrayList<Item>();
        //然后将itemDetail和product整合进这个list
        for(int i=0;i<itemDetails.size();i++) {
            Item item = new Item();
            //
            item.setItemId(itemDetails.get(i).getItemId());
            item.setListPrice(itemDetails.get(i).getListPrice());
            item.setUnitCost(itemDetails.get(i).getUnitCost());
            item.setSupplierId(itemDetails.get(i).getSupplierId());
            item.setStatus(itemDetails.get(i).getStatus());
            item.setAttribute1(itemDetails.get(i).getAttribute1());
            item.setAttribute2(itemDetails.get(i).getAttribute2());
            item.setAttribute3(itemDetails.get(i).getAttribute3());
            item.setAttribute4(itemDetails.get(i).getAttribute4());
            item.setAttribute5(itemDetails.get(i).getAttribute5());

            //查product表
            Product product = productMapper.selectById(productId);

            //配对
            item.setProduct(product);

            //加入list
            itemList.add(item);

        }
        System.out.println("加入list成功啦，里面的ItemId有：");
        for (Item item:itemList) {
            System.out.println(item.getItemId());
        }

        return itemList;
    }

    //根据商品id获得商品(item,product,inventory)
    public Item getItem(String itemId) {
        ItemDetail itemDetail = itemDetailMapper.selectById(itemId);
        System.out.println("此商品的productID是：" + itemDetail.getProductId());
        Product product = productMapper.selectById(itemDetail.getProductId());
        System.out.println("此商品的name是：" + product.getName());
        Inventory inventory = inventoryMapper.selectById(itemId);
        System.out.println("此商品的quantity是：" + inventory.getQuantity());
        Item item = new Item();
        item.setItemId(itemDetail.getItemId());
        item.setListPrice(itemDetail.getListPrice());
        item.setUnitCost(itemDetail.getUnitCost());
        item.setSupplierId(itemDetail.getSupplierId());
        item.setStatus(itemDetail.getStatus());
        item.setAttribute1(itemDetail.getAttribute1());
        item.setAttribute2(itemDetail.getAttribute2());
        item.setAttribute3(itemDetail.getAttribute3());
        item.setAttribute4(itemDetail.getAttribute4());
        item.setAttribute5(itemDetail.getAttribute5());

        item.setQuantity(inventory.getQuantity());

        //配对
        item.setProduct(product);

        System.out.println("此处输出一个item对象的属性，以此检查是否整合成功，此商品listprice为：" + item.getListPrice());

        return item;
    }

    /************
     *
     * 做后台管理时，这里还需添加更新库存的方法，调用inventoryMapper的update方法即可
     * 测试里有例子
     *
     * ****************/
    //某个商品是否有内存
    public boolean isItemInStock(String itemId) {
        Inventory inventory = inventoryMapper.selectById(itemId);
        boolean result = false;
        int quantity = inventory.getQuantity();
        if(quantity>0) {
            result = true;
            System.out.println("库存可多啦，有" + quantity + "个呢");
        }
        else System.out.println("商品库存不足，无法购买");
        return result;
    }

//    //根据用户id找购物车
//    public Cart getCartByUsername(String username){
//        return cartDAO.getCartByUsername(username);
//    }
//
//    //根据用户删除购物车
//    public boolean deleteCartByUsername(String username){
//        return cartDAO.deleteCartByUsername(username);
//    }
//
//    //插入商品到购物车
//    public boolean insertCartItem(CartItem cartItem, String username){
//        return cartDAO.insertCart(cartItem,username);
//    }
//
//    //更新购物车的商品
//    public boolean updateCartItem(String username ,String itemId,int quantity){
//        return cartDAO.updateCartItem(username ,itemId,quantity);
//    }
//
//
//    //删除购物车的商品
//    public boolean removeCartItem(String username ,String itemId){
//        return cartDAO.removeCartItem(username,itemId);
//    }


}
